#include "minunit.h"
#include <lcthw/stack.h>
#include <assert.h>

static Stack *stack = NULL;
char *tests[] = {"test1 data", "test2 data", "test3 data"};
#define NUM_TESTS 3

// Stack *Stack_create() {
//     Stack *stack=(Stack *)malloc(sizeof(Stack)); 
//     stack->count = 0;stack->top = NULL; return stack;
// }
// void Stack_push(Stack *stack,void *val) {
//     StackNode *new_node = malloc(sizeof(StackNode));
//     assert(new_node != NULL);
//     new_node->value =val;
//     new_node->next = stack->top; 
//     stack->top = new_node;
//     stack->count++;
//     return;
// }

// void *Stack_pop(Stack *stack) {
//     StackNode *new_node = malloc(sizeof(StackNode)); 
//     assert(new_node != NULL);
//     new_node->value = stack->top->value; 
//     new_node->next = NULL;
//     stack->top = stack->top->next; 
//     Stack_count(stack) = (Stack_count(stack) >0 ?((Stack_count(stack))-1):0);
//     return new_node->value;
// }
char *test_create()
{
    stack = Stack_create();
    mu_assert(stack != NULL, "Failed to create stack.");

    return NULL;
}

char *test_destroy()
{
    mu_assert(stack != NULL, "Failed to make stack #2");
    Stack_destroy(stack);

    return NULL;
}

char *test_push_pop()
{
    int i = 0;
    for(i = 0; i < NUM_TESTS; i++) {
        Stack_push(stack, tests[i]);
        mu_assert(Stack_peek(stack) == tests[i], "Wrong next value.");
        printf("%s\n",(char *)stack->top->value);
    }

    mu_assert(Stack_count(stack) == NUM_TESTS, "Wrong count on push.");

    STACK_FOREACH(stack, cur) {
        debug("VAL: %s", (char *)cur->value);
    }

    for(i = NUM_TESTS - 1; i >= 0; i--) {
        char *val = Stack_pop(stack);
        printf("%s\n",val);
        mu_assert(val == tests[i], "Wrong value on pop.");
    }

    mu_assert(Stack_count(stack) == 0, "Wrong count after pop.");

    return NULL;
}

char *all_tests() {
    mu_suite_start();

    mu_run_test(test_create);
    mu_run_test(test_push_pop);
    mu_run_test(test_destroy);

    return NULL;
}

RUN_TESTS(all_tests);